<?php
/**
 * Контроллер пользовательских форм фронт-енда
 * 
 * @category Phorm
 * @package Controller_Module_Default
 * @name /Phorm/Controller/Module/Default/Forms.php
 * @copyright phorm.ru
 * @since 04.01.2011
 * @author vladimir@kachalov.net
 */

class Phorm_Controller_Module_Default_Forms extends Phorm_Controller_Controller {
	
	/**
	 * Экземпляр класса пользовательских форм
	 * @var Phorm_Resource_Forms
	 */
	private $Forms;
	
	
	/**
	 * Предиспетчер
	 * 
	 * @return void
	 */
	
	public function preDispatch() {
		
		$this->Forms = new Phorm_Resource_Forms();
		
	}
	
	/**
	 * Вывод и обработка формы
	 * 
	 * @return void
	 */
		
	public function viewAction() {
			
		if(isset($this->_params["resourceinfo"]["resourceid"]) && $FormInfo=$this->Forms->getFormInfo($this->_params["resourceinfo"]["resourceid"])) {
			
			$FormInfo += $this->_params["resourceinfo"];
				
			if(!empty($FormInfo["seotitle"])) $this->view->assign("Title",$FormInfo["seotitle"]);
			if(!empty($FormInfo["seodescription"])) $this->view->assign("Description",$FormInfo["seodescription"]);
			if(!empty($FormInfo["seokeywords"])) $this->view->assign("Keywords",$FormInfo["seokeywords"]);
				
			$this->view->assign("Header",$FormInfo["resourcename"]);
				
			$this->view->assign("FormInfo",$FormInfo);
				
			$form = $this->Forms->buildForm($FormInfo["fields"]);
				
			if($_POST) {
					
				if($form->isValid($_POST)) {
					
					$FormData = $form->getValues();
						
					$EntryId = $this->Forms->saveEntry($FormData,$FormInfo);
						
					$this->view->assign("Success",array($FormInfo["successtext"]));
					
				} else {
						
					$this->view->assign("Form",$form);
						
					$this->view->assign("Errors",array($this->_translate->_("При заполнении формы были обнаружены ошибки")));
						
				}
					
			} else {
					
				$this->view->assign("Form",$form);
					
			}
				
		} else {
				
			return $this->_forward("notfound","error","default",array("action"=>"notfound","controller"=>"error","module"=>"default"));
				
		}
						
	}
	
}